package com.white.controller;

import com.white.bean.*;
import com.white.mapper.ThumbsUpMapper;
import com.white.service.ArticleService;
import com.white.service.CommentService;
import com.white.service.FollowService;
import com.white.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Controller
public class DynamicController {
    @Autowired
    CommentService commentService;
    @Autowired
    ArticleService articleService;
    @Autowired
    ThumbsUpMapper thumbsUpMapper;
    @Autowired
    UserService userService;
    @Autowired
    FollowService followService;

    /**
     * 1.发表评论：评论的内容、文章信息
     * 2.发表文章：文章信息
     * 3.点赞文章：点赞时间、文章信息
     * 4.关注：关注信息
     * @param userId
     * @return
     */
    @GetMapping("/getDynamic/{userId}")
    @ResponseBody
    public Object getDynamic(@PathVariable("userId") Long userId) {
        Map<String, Object> ans = new HashMap<>();
        /*获取评论信息*/
        List<Comment> comments = commentService.getCommentByUserId(userId);
        List<Object> commentList = new ArrayList<>();
        for (Comment comment : comments) {
            Map<String, Object> map = new HashMap<>();
            Article article = articleService.getById(comment.getArticleId());
            article.setContent(null);
            map.put("comment", comment);
            map.put("article", article);
            commentList.add(map);
        }
        ans.put("comments", commentList);
        List<Article> articles = articleService.getLikeArticleByUserId(userId);
        for (Article article : articles) {
            article.setContent(null);
        }
        ans.put("articles", articles);

        /*获取点赞信息*/
        List<ThumbsUp> thumbs = thumbsUpMapper.getLikeByUserId(userId);
        List<Object> thumbList = new ArrayList<>();
        for (ThumbsUp thumb : thumbs) {
            Map<String, Object> map = new HashMap<>();
            Article article = articleService.getById(thumb.getBeThumbsUpId());
            article.setContent(null);
            map.put("like", thumb);
            map.put("article", article);
            thumbList.add(map);
        }
        ans.put("likes", thumbList);
        List<Object> followList = new ArrayList<>();
        List<FollowView> follows = followService.getFollowById(userId);
        for (FollowView follow : follows) {
            Map<String, Object> map = new HashMap<>();
            User user = userService.getById(follow.getAuthorId());
            map.put("follow", follow);
            map.put("user", user);
            followList.add(map);
        }
        ans.put("follows", followList);
        return ans;
    }
}
